/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.IllegalOrphanException;
import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Etudier;
import java.util.ArrayList;
import java.util.Collection;
import com.applicatioN.entities.Exiger4;
import com.applicatioN.entities.Specialite;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class SpecialiteJpaController implements Serializable {

    public SpecialiteJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Specialite specialite) throws PreexistingEntityException, Exception {
        if (specialite.getEtudierCollection() == null) {
            specialite.setEtudierCollection(new ArrayList<Etudier>());
        }
        if (specialite.getExiger4Collection() == null) {
            specialite.setExiger4Collection(new ArrayList<Exiger4>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Etudier> attachedEtudierCollection = new ArrayList<Etudier>();
            for (Etudier etudierCollectionEtudierToAttach : specialite.getEtudierCollection()) {
                etudierCollectionEtudierToAttach = em.getReference(etudierCollectionEtudierToAttach.getClass(), etudierCollectionEtudierToAttach.getEtudierPK());
                attachedEtudierCollection.add(etudierCollectionEtudierToAttach);
            }
            specialite.setEtudierCollection(attachedEtudierCollection);
            Collection<Exiger4> attachedExiger4Collection = new ArrayList<Exiger4>();
            for (Exiger4 exiger4CollectionExiger4ToAttach : specialite.getExiger4Collection()) {
                exiger4CollectionExiger4ToAttach = em.getReference(exiger4CollectionExiger4ToAttach.getClass(), exiger4CollectionExiger4ToAttach.getExiger4PK());
                attachedExiger4Collection.add(exiger4CollectionExiger4ToAttach);
            }
            specialite.setExiger4Collection(attachedExiger4Collection);
            em.persist(specialite);
            for (Etudier etudierCollectionEtudier : specialite.getEtudierCollection()) {
                Specialite oldSpecialite1OfEtudierCollectionEtudier = etudierCollectionEtudier.getSpecialite1();
                etudierCollectionEtudier.setSpecialite1(specialite);
                etudierCollectionEtudier = em.merge(etudierCollectionEtudier);
                if (oldSpecialite1OfEtudierCollectionEtudier != null) {
                    oldSpecialite1OfEtudierCollectionEtudier.getEtudierCollection().remove(etudierCollectionEtudier);
                    oldSpecialite1OfEtudierCollectionEtudier = em.merge(oldSpecialite1OfEtudierCollectionEtudier);
                }
            }
            for (Exiger4 exiger4CollectionExiger4 : specialite.getExiger4Collection()) {
                Specialite oldSpecialite1OfExiger4CollectionExiger4 = exiger4CollectionExiger4.getSpecialite1();
                exiger4CollectionExiger4.setSpecialite1(specialite);
                exiger4CollectionExiger4 = em.merge(exiger4CollectionExiger4);
                if (oldSpecialite1OfExiger4CollectionExiger4 != null) {
                    oldSpecialite1OfExiger4CollectionExiger4.getExiger4Collection().remove(exiger4CollectionExiger4);
                    oldSpecialite1OfExiger4CollectionExiger4 = em.merge(oldSpecialite1OfExiger4CollectionExiger4);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findSpecialite(specialite.getSpecialite()) != null) {
                throw new PreexistingEntityException("Specialite " + specialite + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Specialite specialite) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Specialite persistentSpecialite = em.find(Specialite.class, specialite.getSpecialite());
            Collection<Etudier> etudierCollectionOld = persistentSpecialite.getEtudierCollection();
            Collection<Etudier> etudierCollectionNew = specialite.getEtudierCollection();
            Collection<Exiger4> exiger4CollectionOld = persistentSpecialite.getExiger4Collection();
            Collection<Exiger4> exiger4CollectionNew = specialite.getExiger4Collection();
            List<String> illegalOrphanMessages = null;
            for (Etudier etudierCollectionOldEtudier : etudierCollectionOld) {
                if (!etudierCollectionNew.contains(etudierCollectionOldEtudier)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Etudier " + etudierCollectionOldEtudier + " since its specialite1 field is not nullable.");
                }
            }
            for (Exiger4 exiger4CollectionOldExiger4 : exiger4CollectionOld) {
                if (!exiger4CollectionNew.contains(exiger4CollectionOldExiger4)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Exiger4 " + exiger4CollectionOldExiger4 + " since its specialite1 field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Etudier> attachedEtudierCollectionNew = new ArrayList<Etudier>();
            for (Etudier etudierCollectionNewEtudierToAttach : etudierCollectionNew) {
                etudierCollectionNewEtudierToAttach = em.getReference(etudierCollectionNewEtudierToAttach.getClass(), etudierCollectionNewEtudierToAttach.getEtudierPK());
                attachedEtudierCollectionNew.add(etudierCollectionNewEtudierToAttach);
            }
            etudierCollectionNew = attachedEtudierCollectionNew;
            specialite.setEtudierCollection(etudierCollectionNew);
            Collection<Exiger4> attachedExiger4CollectionNew = new ArrayList<Exiger4>();
            for (Exiger4 exiger4CollectionNewExiger4ToAttach : exiger4CollectionNew) {
                exiger4CollectionNewExiger4ToAttach = em.getReference(exiger4CollectionNewExiger4ToAttach.getClass(), exiger4CollectionNewExiger4ToAttach.getExiger4PK());
                attachedExiger4CollectionNew.add(exiger4CollectionNewExiger4ToAttach);
            }
            exiger4CollectionNew = attachedExiger4CollectionNew;
            specialite.setExiger4Collection(exiger4CollectionNew);
            specialite = em.merge(specialite);
            for (Etudier etudierCollectionNewEtudier : etudierCollectionNew) {
                if (!etudierCollectionOld.contains(etudierCollectionNewEtudier)) {
                    Specialite oldSpecialite1OfEtudierCollectionNewEtudier = etudierCollectionNewEtudier.getSpecialite1();
                    etudierCollectionNewEtudier.setSpecialite1(specialite);
                    etudierCollectionNewEtudier = em.merge(etudierCollectionNewEtudier);
                    if (oldSpecialite1OfEtudierCollectionNewEtudier != null && !oldSpecialite1OfEtudierCollectionNewEtudier.equals(specialite)) {
                        oldSpecialite1OfEtudierCollectionNewEtudier.getEtudierCollection().remove(etudierCollectionNewEtudier);
                        oldSpecialite1OfEtudierCollectionNewEtudier = em.merge(oldSpecialite1OfEtudierCollectionNewEtudier);
                    }
                }
            }
            for (Exiger4 exiger4CollectionNewExiger4 : exiger4CollectionNew) {
                if (!exiger4CollectionOld.contains(exiger4CollectionNewExiger4)) {
                    Specialite oldSpecialite1OfExiger4CollectionNewExiger4 = exiger4CollectionNewExiger4.getSpecialite1();
                    exiger4CollectionNewExiger4.setSpecialite1(specialite);
                    exiger4CollectionNewExiger4 = em.merge(exiger4CollectionNewExiger4);
                    if (oldSpecialite1OfExiger4CollectionNewExiger4 != null && !oldSpecialite1OfExiger4CollectionNewExiger4.equals(specialite)) {
                        oldSpecialite1OfExiger4CollectionNewExiger4.getExiger4Collection().remove(exiger4CollectionNewExiger4);
                        oldSpecialite1OfExiger4CollectionNewExiger4 = em.merge(oldSpecialite1OfExiger4CollectionNewExiger4);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = specialite.getSpecialite();
                if (findSpecialite(id) == null) {
                    throw new NonexistentEntityException("The specialite with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Specialite specialite;
            try {
                specialite = em.getReference(Specialite.class, id);
                specialite.getSpecialite();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The specialite with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Etudier> etudierCollectionOrphanCheck = specialite.getEtudierCollection();
            for (Etudier etudierCollectionOrphanCheckEtudier : etudierCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Specialite (" + specialite + ") cannot be destroyed since the Etudier " + etudierCollectionOrphanCheckEtudier + " in its etudierCollection field has a non-nullable specialite1 field.");
            }
            Collection<Exiger4> exiger4CollectionOrphanCheck = specialite.getExiger4Collection();
            for (Exiger4 exiger4CollectionOrphanCheckExiger4 : exiger4CollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Specialite (" + specialite + ") cannot be destroyed since the Exiger4 " + exiger4CollectionOrphanCheckExiger4 + " in its exiger4Collection field has a non-nullable specialite1 field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(specialite);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Specialite> findSpecialiteEntities() {
        return findSpecialiteEntities(true, -1, -1);
    }

    public List<Specialite> findSpecialiteEntities(int maxResults, int firstResult) {
        return findSpecialiteEntities(false, maxResults, firstResult);
    }

    private List<Specialite> findSpecialiteEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Specialite.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Specialite findSpecialite(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Specialite.class, id);
        } finally {
            em.close();
        }
    }

    public int getSpecialiteCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Specialite> rt = cq.from(Specialite.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
